package presentation.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JTable;

import presentation.data.DataUtil;
import presentation.data.MessagesUtil;
import presentation.data.ViewOrdersData;
import presentation.gui.EbazaarMainFrame;
import presentation.gui.SelectOrderWindow;
import presentation.gui.ViewOrderDetailsWindow;
import business.exceptions.BackendException;
import business.exceptions.UIException;
import business.exceptions.UnauthorizedException;
import business.externalinterfaces.Order;

public class ViewOrdersUIControl {
	ViewOrdersData data;
	class ViewOrderDetailsListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
        	data = ViewOrdersData.getInstance();
            JTable table = selectOrderWindow.getTable();
        	try {
        		Order selectedOrder = data.getSelectedOrder(table);
        		//get the order items related to this order
                viewOrderDetailsWindow  = new ViewOrderDetailsWindow();
                //remember to update model
                mainFrame.getDesktop().add(viewOrderDetailsWindow);
                viewOrderDetailsWindow.setVisible(true);
        	} catch(UIException e) {
        		MessagesUtil.displayErrorMsg(selectOrderWindow, e.getMessage());
        	} catch(BackendException e) {
        		MessagesUtil.displayErrorMsg(selectOrderWindow, e.getMessage());
        	}
        }
	}
	class CancelViewOrdersListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
        	selectOrderWindow.setVisible(false);
        	BrowseSelectUIControl.getInstance().makeMainFrameVisible();

        }
	}
	
	///// control of ViewOrderDetailsWindow
	class OrderDetailsOkListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
			selectOrderWindow.setVisible(true);			
			viewOrderDetailsWindow.dispose();
        }
	}	
    
	class SelectOrderActionListener implements ActionListener,Callback {
        ViewOrdersData data = ViewOrdersData.getInstance();
		public void doUpdate() {
			if(selectOrderWindow != null){		
						
			}
		}	    
		public void actionPerformed(ActionEvent e) {
			selectOrderWindow = new SelectOrderWindow();
			try {
        		Authorization.checkAuthorization(selectOrderWindow, DataUtil.custIsAdmin());
        	} catch(UnauthorizedException ex) {   
            	MessagesUtil.displayErrorMsg(mainFrame, 
            			ex.getMessage());
            	return;
            }	
        	
        	//user must be asked to login here; if not logged in go to login
        	//if already logged in, proceed
        	//see how this is done in ProceedToCheckout in CheckoutUIControl class
			
			//needs to log in
			if(!DataUtil.isLoggedIn()) {
	       	   //require login
			}
			else {//already logged in
				//proceed -- get the orderhistory from customer; get customer from SessionCache
				
			}
			mainFrame.getDesktop().add(selectOrderWindow);
			selectOrderWindow.setVisible(true);		
		}

	}       
 
    ///////// PUBLIC INTERFACE -- for getting instances of listeners ///
	public ActionListener getViewOrderDetailsListener(SelectOrderWindow w){
	    return (new ViewOrderDetailsListener());
	}    
	public ActionListener getCancelViewOrdersListener(SelectOrderWindow w){
	    return (new CancelViewOrdersListener());
	}  
	
	public ActionListener getOrderDetailsOkListener(ViewOrderDetailsWindow w){
	    return (new OrderDetailsOkListener());
	}  	
    public ActionListener getSelectOrderActionListener(EbazaarMainFrame f){
        return (new SelectOrderActionListener());
    }   
    
    ////////  PUBLIC ACCESSORS to register screens controlled by this class////
	public void setSelectOrderWindow(SelectOrderWindow w){
	    selectOrderWindow = w;
	}      
	public void setViewOrderDetailsWindow(ViewOrderDetailsWindow w){
	    viewOrderDetailsWindow = w;
	}     
	public void setMainFrame(EbazaarMainFrame w){
	    mainFrame = w;
	}    
    /////// screens -- private references
    private SelectOrderWindow selectOrderWindow;
    private ViewOrderDetailsWindow viewOrderDetailsWindow;
    private EbazaarMainFrame mainFrame;
	
	  
    /////// make this class a singleton
    private static ViewOrdersUIControl instance = new ViewOrdersUIControl();
    public static ViewOrdersUIControl getInstance(){
        return instance;
    }
    private ViewOrdersUIControl(){}  
}
